package com.xiaoma.demo.deeplink;

import android.net.Uri;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UrlGenerator {

    public static final String generateUrl(String path, Map<String, Object> params) {

        List<String> paramsList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            paramsList.add(key + "=" + value);
        }

        StringBuilder sb = new StringBuilder();
        for (int index = 0; index < paramsList.size(); index++) {
            sb.append(paramsList.get(index));
            if (index < paramsList.size() - 1) {
                sb.append("&");
            }
        }
        String paramsStr = sb.toString();

        return new Uri.Builder()
                .scheme("xiaoma")
                .encodedAuthority("store.xiaoma.com")
                .encodedPath("gate")
                .appendQueryParameter("path", path)
                .appendQueryParameter("params", paramsStr)
                .build()
                .toString();
    }

}
